<template>
  <div class="sysConfig">
    <div class="sysConfig-top">
      <el-button type="success" @click='addActivity'>申请活动</el-button>
    </div>
    <el-table
      ref="tableRef"
      :data="tableData"
      border
      style="width: 100%"
      v-loading="loading"
      @row-click='lookDetails'
      @sort-change='tableSortChange'
      height="88%">
      <el-table-column show-overflow-tooltip label="ID" width="60" align="left">
        <template slot-scope="scope">{{(scope.$index+1)+(currentPage-1)*tableSize }}</template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="活动名称" align="left" prop='name' sortable='custom' min-width="130"></el-table-column>
      <el-table-column show-overflow-tooltip label="奖励金额(元)" align="left" prop='reward' sortable='custom' min-width="130"></el-table-column>
      <el-table-column show-overflow-tooltip label="活动状态" align="left" prop='status' sortable='custom' min-width="130">
        <template slot-scope="scope">
          {{ scope.row.status | activityStatus }}
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="开始时间" align="left" prop='created_at' sortable='custom'  min-width="160">
        <template slot-scope="scope">
          {{ scope.row.condition.start_time }}
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="结束时间" align="left" prop='created_at' sortable='custom'  min-width="160">
        <template slot-scope="scope">
          {{ scope.row.condition.end_time }}
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="创建时间" align="left" prop='created_at' sortable='custom' min-width="160"></el-table-column>
      <el-table-column show-overflow-tooltip label="更新时间" align="left" prop='updated_at' sortable='custom' min-width="160"></el-table-column>
      <!-- <el-table-column
        fixed="right"
        align="center"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button @click.stop="editAction(scope.row)" v-no-more-click:3000 type="warning" size="small">修改活动</el-button>
          <el-button v-if="scope.row.status == 3 && $store.state.user.role_id == 1" @click.stop="review(scope.row)" type="warning"  size="small">审核</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <el-pagination
      class="pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
			:page-sizes="pageSizes"
      :page-size="Number(pageSize)"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <addActivityDialog ref='addActivityDialog' @getData='getActivityList'></addActivityDialog>


    <!-- 查看活动 -->
    <lookDetails ref="lookDetails" @getData='getActivityList'></lookDetails>
  </div>
</template>

<script>
import addActivityDialog from './components/addActivityDialog'
import lookDetails from './components/lookDetails'
import { getMyInfo } from "~/api/user"
export default {
  data () {
    return {
      paramsByActivity: {
      },
      tableData: [],
      loading: false,
      pageSize: 25, //一页可以几条
      pageSizes: [25, 50], //一页可以几条,
      currentPage: 1, //目前在第几页
      current_page: 0,
      tableSize: 25, //一页几条
      total: 0,
      myInfo: {
        activity_check: 0
      }
    }
  },
  components: {
    addActivityDialog,
    lookDetails
  },
  created () {
    this.getActivityList()
  },
  methods: {
    async getMyinfo (params) {
      let myInfo = await getMyInfo(this.$axios, params);
      if (myInfo.data.status == 'success') {
        this.$store.commit("setRoleInfo",myInfo.data.data)
        this.$store.commit("setUser",myInfo.data.data)
        this.myInfo = myInfo.data.data
        this.roleID = this.myInfo.role_id
      }
    },
    // 查看活动
    lookDetails(row) {
      this.$refs['lookDetails'].show(row)
    },
    getActivityList (args) {
      let params = {
        perPage: this.pageSize,
        page: this.currentPage,
      }
      params = {...params, ...this.paramsByActivity}
      this.$axios.get('/api/activity', {
        params: params,
      }).then(res => {
        if (res.data.code == 200) {
          this.loading = false
          for(let i = 0; i <res.data.data.length; i++) {
            res.data.data[i].condition = JSON.parse(res.data.data[i].condition)
          }
          this.tableData = res.data.data
          this.pageSize = res.data.page.per_page
          this.total = res.data.page.total
          this.getMyinfo()
        }
      })
    },
    // 添加活动
    addActivity () {
      this.$refs.addActivityDialog.show()
    },

    handleSizeChange (size) {
      this.pageSize = size
      this.paramsByActivity.page = 1
      this.getActivityList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.paramsByActivity.page = val
      this.getActivityList()
    },
    tableSortChange (column) {
      if (column.order == 'ascending') {
        this.paramsByActivity.sort_str = column.prop
        this.paramsByActivity.sort_order = 'asc'
      } else if (column.order == 'descending') {
        this.paramsByActivity.sort_str = column.prop
        this.paramsByActivity.sort_order = 'desc'
      } else {
        delete this.paramsByActivity.sort_str
        delete this.paramsByActivity.sort_order
      }
      this.loading = true
      this.currentPage = 1
      this.getActivityList()
    },
  }
}
</script>

<style lang='less' type='text/css'>
  .sysConfig {
    height: 100%;
    &-top{
      margin-bottom: 10px;
    }
  }
</style>
